﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CompanyManagementDLL;
using CompanyManagementDLL.DLL;
using CompanyManagementDLL.MetaData;
using log4net;

namespace CompanyManagement.Controllers
{
    public class LoginController : Controller
    {
        UserDLL dllUser = new UserDLL();
        private static readonly ILog Log = LogManager.GetLogger(typeof(LoginController));

        //
        // GET: /Login/

        public ActionResult Index()
        {
            if (Utility.checkCookie())
            {
                return RedirectToAction("Index", "Home");
            }
            return PartialView();
        }

        [HttpPost]
        public ActionResult Index(UserData userdata)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (dllUser.ValidateUser(userdata))
                    {
                        UserMetaData usermetadata = dllUser.GetUserByUsername(userdata.UserName);
                        HttpCookie aCookie = new HttpCookie("UserCookie");
                        aCookie.Values["zUserID"] = Convert.ToString(usermetadata.UserID);
                        aCookie.Values["zRoleID"] = Convert.ToString(usermetadata.RoleID);
                        aCookie.Values["zUserName"] = Convert.ToString(usermetadata.UserName);
                        aCookie.Values["zPassword"] = Convert.ToString(usermetadata.Password);
                        aCookie.Values["zRoleName"] = Convert.ToString(new RoleDLL().GetRoleByID(usermetadata.RoleID ?? 0).RoleName);
                        aCookie.Values["LastLoginDate"] = Convert.ToString(usermetadata.LastLoginDate);
                        aCookie.Values["ProfilePicName"] = Convert.ToString(usermetadata.ProfilePicPath);
                        aCookie.Expires = DateTime.Now.AddDays(1);
                        HttpCookie lcookie = new HttpCookie("LoginCookie");
                      
                        usermetadata.LastLoginDate = DateTime.Now;
                        dllUser.UpdateUser(usermetadata);
                        if (userdata.RememberMe == true)
                        {
                            aCookie.Expires = DateTime.Now.AddDays(365);
                        }

                        Response.Cookies.Add(lcookie);
                        Response.Cookies.Add(aCookie);
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError("", "The user name or password provided is incorrect.");
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return PartialView(userdata);

        }

        public ActionResult Logout()
        {
            try
            {
                if (Request.Cookies["UserCookie"] != null)
                {
                    HttpCookie myCookie = new HttpCookie("UserCookie");
                    myCookie.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(myCookie);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }

            return RedirectToAction("Index");
        }

        public string GetPassword(string emailid)
        {
            string password=string.Empty;
            try
            {
                var userdata=dllUser.GetUserByEmailAddress(emailid);
                if(userdata == null)
                {
                    password="Invalid Email Address.";
                }
                else
                {
                    password=userdata.Password;
                }

            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }

            return password;
        }

    }
}
